const Router = require('koa-router')
const router = new Router()
const callCloudDB = require('../utils/callCloudDB')
const callCloudStorage = require('../utils/callCloudStorage')

// 获取博客列表
router.get('/list', async(ctx, next) => {
  const params = ctx.query
  const query = `
    db.collection('blog').skip(${params.start}).limit(${params.count}).orderBy('createtime', 'desc').get()
  `
  const result = await callCloudDB(ctx, 'databasequery', query)
  ctx.body = {
    code: 20000,
    data: result.data
  }
})
// 删除指定博客
router.post('/del', async(ctx, next) => {
  const { body } = ctx.request
  // 删除blog
  const query = `db.collection('blog').doc('${body._id}').remove()`
  const delBlogRes = await callCloudDB(ctx, 'databasedelete', query)
  // 删除blog-comment
  const queryComment = `db.collection('blog-comment').where({
    blogId: '${body._id}'
  }).remove()`
  const delCommentRes = await callCloudDB(ctx, 'databasedelete', queryComment)
  // 删除图片
  const delStorageRes = await callCloudStorage.delete(ctx, body.img)
  console.log('delBlogRes :>> ', delBlogRes);
  console.log('delCommentRes :>> ', delCommentRes);
  console.log('delStorageRes :>> ', delStorageRes);
  ctx.body = {
    code: 20000,
    data: {
      delBlogRes: delBlogRes.data.deleted,
      delCommentRes: delCommentRes.data.deleted,
      delStorageRes: delStorageRes.data.fileid_list
    }
  }
})
module.exports = router
